# -*- coding: utf-8 -*-
'''
This class makes the bridge betwen the UI and the other classes
Created on 2011/03/29
@author: Edgar Pereira
Nº 6207
'''

import time
from MySoup import MySoup
from MyDataBase import MyDataBase
from MyPlot import MyPlot
from MyServer import MyServer
from MyHTML import MyHTML
from MyCSV import MyCSV
import webbrowser
from MyMessages import *
import os

class MyBridge():
    #-------------------------------------------------------
    #Class constructor
    #-------------------------------------------------------
    def __init__(self):
        print ('My Bridge pid = {0}'.format(os.getpid()))
        self.ms = MyMessages()
        pass
    
    #-------------------------------------------------------
    #To make the database
    #-------------------------------------------------------
    def make_data(self, ano):
        t1 = time.clock()
        myDB = MyDataBase()
        if os.path.exists('rebides200{0}.db'.format(ano)):
            self.ms.ShowMessage4("200{0} year's data already colected".format(ano), "Rebides")
            print ("200{0} year's data already colected".format(ano))
            pass
        else:
            myDB.preencher_tabelas_gerais(ano)
            self.ms.ShowMessage1("All 200{0} year's data colected".format(ano), "Rebides")
            print ("All 200{0} year's data colected")
            pass
        t2 = time.clock()
        print "tempo de execução: ", t2-t1
        pass
    
    
    #-------------------------------------------------------
    #To make the graphs
    #-------------------------------------------------------
    def make_graph(self, graph, ano):
        MyPlot(graph, ano)
        print ('Graphic generated')
        pass
    
    
    #-------------------------------------------------------
    #Starts the http server
    #-------------------------------------------------------    
    def start_server(self):
        self.msrvr = MyServer()
        self.msrvr.start()
        self.ms.ShowMessage1("Server Started", "Rebides")
        pass
    
    
    #-------------------------------------------------------
    #Starts the http server
    #-------------------------------------------------------    
    def stop_server(self):
        self.msrvr.stop()
        self.ms.ShowMessage4("Server Stoped", "Rebides")
        pass
    
    
    #-------------------------------------------------------
    #Creates the HTML files
    #------------------------------------------------------    
    def make_html(self):
        mhtml = MyHTML()
        mhtml.criar_index()
        mhtml.tot_prof_ano()
        mhtml.total_prof_est_ano()
        mhtml.total_prof_grau_ano()
        mhtml.total_prof_grau_est_ano()
        for i in range(0, 10):
            mhtml.list_inst_ano(i)
        for i in range(0, 10):
            mhtml.list_prof_grau_ano(i)
        self.ms.ShowMessage1("HTML generated", "Rebides")
        print ('HTML generated - OK')
        pass
    
    
    #-------------------------------------------------------
    #Creates the CSV files
    #------------------------------------------------------    
    def make_csv(self):
        mcsv = MyCSV()
        mcsv.total_prof_ano()
        mcsv.total_prof_est_ano()
        mcsv.total_prof_grau_ano()
        mcsv.total_prof_grau_ano()
        mcsv.total_prof_grau_est_ano()
        mcsv.list_est_ano()
        mcsv.list_prof_grau_anos()
        self.ms.ShowMessage1("CSV generated", "Rebides")
        print ('CSV generated - OK')
        pass
    
    
    #-------------------------------------------------------
    #To open the CSV files
    #-------------------------------------------------------
    def csv_Open(self):
        from MyFlOpen import MyFlOpen
        mfo = MyFlOpen()
        mfo.open()
    
    
    #-------------------------------------------------------
    #To open the browser with the html page
    #------------------------------------------------------    
    def html_Open(self):
        url = 'http://127.0.0.1:8000/'
        webbrowser.open(url)

#MyBridge(0)
